home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / assert / assert.c next >
C/C++ Source or Header  |  1994-05-21  |  2KB  |  85 lines

  1. /* Copyright (C) 1991, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <sysdep.h>
  24.  
  25.  
  26. CONST char *__assert_program_name;
  27.  
  28. /* This function, when passed a string containing an asserted
  29.    expression, a filename, and a line number, prints a message
  30.    on the standard error stream of the form:
  31.        a.c:10: foobar: Assertion `a == b' failed.
  32.    It then aborts program execution via a call to `abort'.  */
  33.  
  34. __NORETURN int
  35. DEFUN(__assert_fail, (assertion, file, line, function),
  36.       CONST char *assertion AND
  37.       CONST char *file AND unsigned int line AND CONST char *function)      
  38. {
  39. #ifdef FATAL_PREPARE
  40.   FATAL_PREPARE;
  41. #endif
  42.  
  43.   /* Print the message.  */
  44.   (void) fprintf (stderr, "%s%s%s:%u: %s%sAssertion `%s' failed.\n",
  45.           __assert_program_name ? __assert_program_name : "",
  46.           __assert_program_name ? ": " : "",
  47.           file, line,
  48.           function ? function : "", function ? ": " : "",
  49.           assertion);
  50.   (void) fflush (stderr);
  51.  
  52.   abort ();
  53.  
  54.   /* This function never returns, so making it void would make sense,
  55.      but returning something makes the assert macro easier to write.  */
  56.   return 0;
  57. }
  58.  
  59. #ifdef    HAVE_GNU_LD
  60.  
  61. #include <string.h>
  62. #include <gnu-stabs.h>
  63.  
  64. static void
  65. DEFUN(set_progname, (argc, argv, envp),
  66.       int argc AND char **argv AND char **envp)
  67. {
  68.   char *p;
  69.  
  70.   if (argv && argv[0])
  71.     {
  72.       p = strrchr (argv[0], '/');
  73.       if (p == NULL)
  74.     __assert_program_name = argv[0];
  75.       else
  76.     __assert_program_name = p + 1;
  77.     }
  78.  
  79.   (void) &set_progname;        /* Avoid "defined but not used" warning.  */
  80. }
  81.  
  82. text_set_element (__libc_subinit, set_progname);
  83.  
  84. #endif
  85.